package com.threeox.commonlibrary.ui.view.multipagenavigation;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.threeox.commonlibrary.entity.engine.model.multipage.PageDataMessage;
import com.threeox.commonlibrary.ui.view.inter.engine.multipage.INavigationIndicator;
import com.threeox.commonlibrary.ui.view.inter.engine.multipage.INavigationIndicatorView;
import com.threeox.commonlibrary.ui.view.inter.engine.multipage.OnNavigationListener;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @ClassName: CommonLinearLayoutIndicator
 * 
 * @Description: TODO(自定义LinearLayout滑动指示器)
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间: 2017/8/10 18:37
 * 
 * @version 1.0
 * 
 */
public class CommonLinearLayoutIndicator extends LinearLayout implements INavigationIndicator {

    private final String TAG = this.getClass().getName();

    private int mTabCount;
    private List<INavigationIndicatorView> mViews;
    private List<PageDataMessage> mPageDataMessages;
    private OnNavigationListener mOnNavigationListener;

    public CommonLinearLayoutIndicator(Context context) {
        this(context, null);
    }

    public CommonLinearLayoutIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public INavigationIndicator setPageDataMessages(List<PageDataMessage> pageDataMessages) {
        this.mPageDataMessages = pageDataMessages;
        mTabCount = mPageDataMessages.size();
        generateView();
        return this;
    }

    @Override
    public PageDataMessage getPageDataMessage(int postion) {
        if (EmptyUtils.isNotEmpty(mPageDataMessages)) {
            return mPageDataMessages.get(postion);
        }
        return null;
    }

    @Override
    public List<PageDataMessage> getPageDataMessage() {
        return mPageDataMessages;
    }

    @Override
    public INavigationIndicator onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        return this;
    }

    @Override
    public INavigationIndicator updateMessage(int postion, PageDataMessage message) {
        try {
            mPageDataMessages.set(postion, message);
            mViews.get(postion).setPageDataMessage(message);
        } catch (Exception e) {
            LogUtils.e(TAG, e.getMessage());
        }
        return this;
    }


    @Override
    public INavigationIndicator setOnNavigationListener(OnNavigationListener onNavigationListener) {
        this.mOnNavigationListener = onNavigationListener;
        return this;
    }

    private void generateView() {
        if (getChildCount() > 0) {
            this.removeAllViews();
        }
        int count = mPageDataMessages.size();
        setWeightSum(count);
        mViews = new ArrayList<INavigationIndicatorView>();
        for (int i = 0; i < count; i++) {
            try {
                PageDataMessage message = mPageDataMessages.get(i);
                Constructor constructor = message.getIndicatorViewClass() != null ? message.getIndicatorViewClass().getConstructor(Context.class) : null;
                final INavigationIndicatorView indicatorView = constructor != null ? (INavigationIndicatorView) constructor.newInstance(getContext()) : new CommonButtonIndicatorView(getContext());
                indicatorView.setPageDataMessage(message);
                final int position = i;
                indicatorView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        resetTabs();
                        indicatorView.setSelected(true);
                        if (mOnNavigationListener != null) {
                            mOnNavigationListener.onNavigationClick(position);
                        }
                    }
                });
                if (i == 0) {
                    indicatorView.setSelected(true);
                }
                addView((View) indicatorView);
                mViews.add(indicatorView);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 重置所有的Tab颜色
     */
    private void resetTabs() {
        try {
            for (int i = 0; i < mViews.size(); i++) {
                mViews.get(i).setSelected(false);
            }
        } catch (Exception e) {
        }
    }

}
